package com.cesare.web.admin;

import com.cesare.pojo.Blog;
import com.cesare.pojo.Type;
import com.cesare.pojo.User;
import com.cesare.service.BlogService;
import com.cesare.service.TagService;
import com.cesare.service.TypeService;
import com.cesare.vo.BlogQuery;
import com.cesare.vo.SearchBlog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    //后台博客列表
    @GetMapping("/blogs")
    public String listBlogs(@RequestParam(value = "pageNum",defaultValue = "1",required = false) Integer pageNum, Model model){
        List<Type> types = typeService.getAllType();
        model.addAttribute("types",types);
        PageHelper.startPage(pageNum,5);
        List<Blog> blogs = blogService.getAllBLog();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/blogs";
    }

    //跳转添加页面
    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());
        model.addAttribute("blog",new Blog());
        return "admin/blogs-input";
    }
    //添加博客或者修改博客
    @PostMapping("/blogs")
    public String postBlog(Blog blog, HttpSession session, RedirectAttributes attributes){
        //设置用户ID
        blog.setUserId(((User)session.getAttribute("user")).getId());
        //设置分类ID
        blog.setType(typeService.getTypeById(blog.getType().getId()));
        blog.setTypeId(blog.getType().getId());
        if(blog.getId()==null){
            //添加
            blogService.addBlog(blog);
            attributes.addFlashAttribute("message","添加成功");
        }else {
            blogService.updateBlog(blog);
            attributes.addFlashAttribute("message","修改成功");
        }
        return "redirect:/admin/blogs";
    }
    //跳转到编辑页面
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable("id") Long id, Model model){
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());
        Blog blog = blogService.getBlogById(id);
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable("id") Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }
    //    搜索博客
    @PostMapping("/blogs/search")
    public String search(SearchBlog searchBlog, Model model,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        List<BlogQuery> blogBySearch = blogService.getBlogBySearch(searchBlog);
        PageHelper.startPage(pageNum, 10);
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogBySearch);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs :: blogList";
    }

}
